/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sunsite.dao;

import java.sql.SQLException;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import sunsite.po.Usr;

/**
 *
 * @author Ruby
 */
public class UsrDaoImpl extends HibernateDaoSupport implements UsrDao{

    public Usr getUsrByUserName(final String userName) throws DataAccessException{
        List<Usr> list = getHibernateTemplate().executeFind(new HibernateCallback() {

            public Object doInHibernate(Session arg0) throws HibernateException, SQLException {
                return arg0.createQuery("from Usr as u where u.userName = ?").setParameter(0, userName).setMaxResults(1).list();
            }
        });
        if(list!=null&&list.size()>0)
            return list.get(0);
        else
            return null;
    }

    public Usr getUsrByUserId(String userId) throws DataAccessException{
        return (Usr)getHibernateTemplate().get(Usr.class, userId);
    }

    public void update(Usr user) throws DataAccessException{
        getHibernateTemplate().update(user);
    }

    public void addUser(Usr user) throws DataAccessException{
        getHibernateTemplate().save(user);
    }

    public void delete(Usr user) throws DataAccessException{
        getHibernateTemplate().delete(user);
    }

    public void delete(String userName) throws DataAccessException{
        Usr user = this.getUsrByUserName(userName);
        getHibernateTemplate().delete(user);
    }
}
